import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;

import java.io.*;

public class ReadPDF {
    public static void main(String[] args) throws Exception {

        String folder_name = "622_经济经纬";

//        System.out.println("test");
//        System.out.println(PdfUtils.getTextPdf1());
//        File f = new File("F:\\爱文本Choice数据\\公告大全\\重大事项\\" +
//                "收购兼并\\pdf\\000001.SZ_平安银行_深发展Ａ：发行股份购买资产暨关联交易报告书摘要_2010-09-15_AN201203010004675532_39016.pdf");
//        Pdf2Txt(f,"D:\\新建文件夹");
//        System.out.println("test");
//        for(int i = 2006; i< 2020;i++){
//            String year = String.valueOf(i);
            int count = 0;
//            File[] f_all = new File("F:\\需要增加的年报\\pdf\\"+year).listFiles();
//            String targetDir = "D:\\2006-2019年报_新版本\\"+year+"年年报_TXT\\";
//        MongoClient mongoClient = new MongoClient("192.168.1.171", 30000);
//        MongoCollection<Document> target = mongoClient.getDatabase("aitexts_nb").getCollection("RawData");
        File[] f_all = new File("D:\\测试临时文件\\ScrapyDownload\\"+folder_name).listFiles();
//        for (File f:f_all) {
//            if(f.getName().endsWith("年报")){
//                System.out.println(f.getName());
//                String year = f.getName().substring(0,4);
//                File[] f_all_in = f.listFiles();
//                for (File f_in:f_all_in) {
//                    System.out.println(f_in.getName());
//                    Document document = new Document("_id",f_in.getName());
//                    try {
//                        String content = PdfUtils.getTextPdf1(f_in);
//
//                        document.append("content",content);
//                    }catch (Exception e){
//                        System.out.println("异常");
////                        Document document = new Document("_id",f_in.getName());
//                    }
//
//                    document.append("year",year);
////                        System.out.println(year);
//                    if(f_in.getName().contains("_")){
//                        String code = f_in.getName().substring(0,f_in.getName().indexOf("_"));
////                            System.out.println(code);
//                        document.append("code",code);
//                    }
//
//                    target.updateOne(new BasicDBObject("_id", document.getString("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));
//
//                }
//            }
//        }
        String targetDir = "D:\\测试临时文件\\黑马TXT\\"+folder_name+"\\";

            for (File f:f_all) {
//            Pdf2Txt_2(f,targetDir+f.getName().replace(".pdf",".txt"));
//            System.out.println(count++);
                if(f.isFile()){
//                System.out.println(f.getName());
//                String Year = f.getName();
                    String name = f.getName().toLowerCase().replace(".pdf",".txt");
                    System.out.println(name);
                    Pdf2Txt_2(f,targetDir+name);
                    System.out.println(count++);
//                File[] f_all_in = f.listFiles();
//                for (File f_in:f_all_in) {
//                    if(f_in.getName().toLowerCase().contains("pdf")){
//                        String name = Year+"_"+f_in.getName().toLowerCase().replace(".pdf",".txt");
//                        System.out.println(name);
//                        Pdf2Txt_2(f_in,targetDir+name);
//                        System.out.println(count++);
////                        File[] f_all_in_in = f_in.listFiles();
////                        for (File f_in_in:f_all_in_in) {
//////                            String name = (GetID(f_in_in.getName()) == null) ? f_in_in.getName().replace(".pdf",".txt"):GetID(f_in_in.getName())+".txt";
//////                            if (GetID(f_in_in.getName()) == null)
//////                                System.out.println(name);
////                            String name = Year+"_"+f.getName().toLowerCase().replace(".pdf",".txt");
////                            Pdf2Txt_2(f_in_in,targetDir+name);
////                            System.out.println(name);
////                        }
//                    }
//                }
                }
            }
        }

//        for (File f:f_all) {
//            File[] f_all_in = f.listFiles();
//            for (File f_in:f_all_in) {
//                String name = f_in.getName();
//                String Year = GetStrNum(name);
//                name = f.getName() +"_"+Year+"_"+name;
//                if(name.endsWith(".pdf")){
//                    name = name.replace("pdf","").replace("PDF","")+"txt";
//                    Pdf2Txt_2(f_in,targetDir+name);
//                }else if(name.endsWith(".txt")){
//                    copyFileUsingFileStreams(f_in,new File(targetDir+name));
//                }
//
//                System.out.println(name);
//            }
//            System.out.println(count++);
//        }
//    }
    private static void copyFileUsingFileStreams(File source, File dest) throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }
    static String GetID(String str){
        String[] s_all = str.split("_");
        for (String s:s_all) {
            if(s.startsWith("AP")||s.startsWith("XB")
                    ||s.startsWith("AS")||s.startsWith("DB")){
                return s;
            }
        }
        return null;
    }
    public static String GetStrNum(String str) {
//        String str = "love23next234csdn3423javaeye";
        str=str.trim();
        String str2="";
        int count = 0;
        if(str != null && !"".equals(str)){
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)>=48 && str.charAt(i)<=57){
                    str2+=str.charAt(i);
                    count++;
                    if(count == 4){
                        break;
                    }
                }

            }

        }
//        System.out.println(str2);
        return str2;
    }
    public static void  Pdf2Txt(File f, String targetDir){
//        String dir =
//        BufferedWriter bfw = null;
        String name =  f.getName().replace("pdf","").replace("PDF","")+"txt";
        int index = name.indexOf(".");
        if(index < 4){
            name = name.substring(index+1);
        }else if(name.contains("、")){
            index = name.indexOf("、");
            if(index < 4){
                name = name.substring(index+1);
            }
        }
        name = name.replace(" ","");
//        System.out.println(index);
//        System.out.println(name);
        String  txtPath = targetDir +"\\"+f.getParentFile().getName()+"_"+ name;
//        System.out.println(txtPath);
        try {
            String content = PdfUtils.getTextPdf1(f);
            writeTxt(txtPath,content);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void  Pdf2Txt_2(File f, String  txtPath){
//        String dir =
//        BufferedWriter bfw = null;
        String name =  f.getName().replace("pdf","").replace("PDF","")+"txt";
        int index = name.indexOf(".");
        if(index < 4){
            name = name.substring(index+1);
        }else if(name.contains("、")){
            index = name.indexOf("、");
            if(index < 4){
                name = name.substring(index+1);
            }
        }
//        name = name.replace(" ","");
//        System.out.println(index);
//        System.out.println(name);
//        String  txtPath = targetDir +"\\"+f.getParentFile().getName()+"_"+ name;
//        System.out.println(txtPath);
        try {
            String content = PdfUtils.getTextPdf1(f);
            writeTxt(txtPath,content);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**使用FileOutputStream来写入txt文件
     * @param txtPath txt文件路径
     * @param content 需要写入的文本
     */
    public static void writeTxt(String txtPath,String content){
        FileOutputStream fileOutputStream = null;
        File file = new File(txtPath);
        try {
            if(file.exists()){
                //判断文件是否存在，如果不存在就新建一个txt
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
